package com.seguriapp.paperworks.gwt.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.seguriapp.entities.Paperwork;
import com.seguriapp.entities.PaperworkAttribute;
import com.seguriapp.entities.PaperworkAttributeCode;
import com.seguriapp.paperworks.gwt.client.activity.PaperworksListActivity;
import com.seguriapp.paperworks.gwt.client.activity.PaperworksListActivity.Display;

public class PaperworksListPage extends Composite implements Display {

  private static PaperworksListUiBinder uiBinder = GWT.create(PaperworksListUiBinder.class);

  interface PaperworksListUiBinder extends UiBinder<Widget, PaperworksListPage> {
  }

  @UiField
  protected ListBox paperworksLB;
  @UiField
  protected Label nameslbl;
  @UiField
  protected Label costlbl;
  @UiField
  protected Label durationlbl;
  @UiField
  protected HTMLPanel objectives;
  @UiField
  protected HTMLPanel filesNeeded;
  @UiField
  protected HTMLPanel observations;
  @UiField
  protected HTMLPanel places;

  private PaperworksListActivity presenter;

  public PaperworksListPage() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  public void setPresenter(PaperworksListActivity presenter) {
    this.presenter = presenter;
  }

  public void initializePaperworkList(final List<Paperwork> paperworksList) {
    paperworksLB.clear();
    if (paperworksList != null) {
      for (final Paperwork paperwork : paperworksList) {
        paperworksLB.addItem(paperwork.getName(), paperwork.getId().toString());
      }
    }
    paperworksLB.addChangeHandler(new ChangeHandler() {

      public void onChange(final ChangeEvent event) {
        updateListData();
      }
    });
    updateListData();
  }

  public void showPaperworkData(final Paperwork paperwork) {
    nameslbl.setText(paperwork.getName());
    costlbl.setText(paperwork.getCost());
    durationlbl.setText(paperwork.getDuration());

    objectives.setVisible(true);
    filesNeeded.setVisible(true);
    observations.setVisible(true);
    places.setVisible(true);

    remoreListContent(objectives);
    remoreListContent(filesNeeded);
    remoreListContent(observations);
    remoreListContent(places);

    if (paperwork.getAttributeList() != null) {
      for (PaperworkAttribute attribute : paperwork.getAttributeList()) {
        final Label attributeLabel = new Label(attribute.getValue());
        if (attribute.getCode().equals(PaperworkAttributeCode.Objective)) {
          objectives.add(attributeLabel);
        }
        if (attribute.getCode().equals(PaperworkAttributeCode.FileNeeded)) {
          filesNeeded.add(attributeLabel);
        }
        if (attribute.getCode().equals(PaperworkAttributeCode.Observation)) {
          observations.add(attributeLabel);
        }
        if (attribute.getCode().equals(PaperworkAttributeCode.Place)) {
          places.add(attributeLabel);
        }
      }
    }
    if (objectives.getWidgetCount() == 1) {
      objectives.setVisible(false);
    }
    if (filesNeeded.getWidgetCount() == 1) {
      filesNeeded.setVisible(false);
    }
    if (observations.getWidgetCount() == 1) {
      observations.setVisible(false);
    }
    if (places.getWidgetCount() == 1) {
      places.setVisible(false);
    }
  }

  private void updateListData() {
    presenter.updatePaperworkData(new Long(paperworksLB.getValue(paperworksLB.getSelectedIndex())));
  }

  private void remoreListContent(final HTMLPanel panel) {
    final Label label = (Label) panel.getWidget(0);
    panel.clear();
    panel.add(label);
  }

}
